﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VersionComparer.Base
{
    public static class StringExtensions
    {

        /// <summary>
        /// Checks if a given string contains an array of parts
        /// </summary>
        /// <param name="string1">string to search in</param>
        /// <param name="parts">list of strings to search for</param>
        /// <param name="ignoreCase">if true, case is ignored</param>
        /// <returns>true if string1 contains all the parts</returns>
        public static bool FindInString(this string string1, string[] parts, bool ignoreCase = true)
        {
            if (parts == null)
                return true;
            StringComparison comparison = ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture;
            int pos = 0;
            foreach (string part in parts)
            {
                pos = string1.IndexOf(part, pos, comparison);
                if (pos < 0)
                    return false;
            }
            return true;
        }


        /// <summary>
        /// Checks if a given string contains an array of parts
        /// </summary>
        /// <param name="string1">string to search in</param>
        /// <param name="parts">list of strings to search for</param>
        /// <param name="ignoreCase">if true, case is ignored</param>
        /// <param name="wildcardChar">char to be used as a wildcard (if not null)</param>
        /// <returns>true if string1 contains all the parts</returns>
        public static bool FindInString(this string string1, string string2, bool ignoreCase = true, char? wildcardChar = '*')
        {
            if (string1 == null)
                return string2 == null;
            if (string2 == null)
                return string1 == null;

            StringComparison comparison = ignoreCase?StringComparison.CurrentCultureIgnoreCase: StringComparison.CurrentCulture;
            if (wildcardChar == null)
                return string2.IndexOf(string1, comparison) > 0;

            return FindInString(string1 , string2.Split(new char[] { wildcardChar.Value }, StringSplitOptions.RemoveEmptyEntries), ignoreCase);
        }
    }
}
